package com.demo.application.service;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.demo.application.dao.OrderRepository;
import com.demo.application.entity.OrderEntity;
import com.demo.application.feign.ApplicationBClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.UUID;

/**
 * Author: kosam
 */
@Service
public class OrderService{

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private ApplicationBClient applicationBClient;

    @LcnTransaction //分布式事务注解
    @Transactional
    public OrderEntity save() {

        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderNum(UUID.randomUUID().toString());
        orderEntity.setOrderMoney(12.45D);
        orderEntity.setCommodityId(30L);
        orderEntity.setOrderCreatetime(new Date());
        orderEntity.setOrderState(0);
        // 扣库存
        applicationBClient.inventoryReduction(30L);
        // 保存订单
        int i = 1 / 0;

        orderRepository.save(orderEntity);
        logger.info(">>>>>>>>>>>>>save order success, orderName={{}}", orderEntity.getOrderNum());
        return orderEntity;
    }

}
